{extend name="app/shop/view/base.html"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-form form-wrap">
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>活动名称：</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required|len" class="layui-input len-long" autocomplete="off" maxlength="40">
        </div>
        <div class="word-aux">
            <p>活动名称最多为25个字符</p>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>活动内容：</label>
        <div class="layui-input-block">
            <div class="layui-input-inline len-short">
                <input type="number" name="divide_num" value="" placeholder="" autocomplete="off" class="layui-input len-short" lay-verify="required|divide_num|count">
            </div>
            <div class="layui-form-mid">名好友，瓜分</div>
            <div class="layui-input-inline len-short">
                <input type="number" name="money" value="" placeholder="" autocomplete="off" class="layui-input len-short" lay-verify="required|money">
            </div>
            <div class="layui-form-mid">元</div>
        </div>
        <div class="word-aux">
            <p>瓜分人数建议5人以下，超过5人存在被微信封禁的风险。</p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>红包总量：</label>
        <div class="layui-input-block">
            <div class="layui-input-inline">
                <input type="number" name="inventory" lay-verify="required|hongNum" autocomplete="off" class="layui-input len-short">
            </div>
            <span class="layui-form-mid">张</span>
        </div>
        <div class="word-aux">
            <p>修改总量时只能增加不能减少，请谨慎设置</p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>瓜分有效期：</label>
        <div class="layui-input-block">
            <div class="layui-input-inline">
                <input type="number" name="divide_time" lay-verify="required|divide_time" autocomplete="off" class="layui-input len-short">
            </div>
            <span class="layui-form-mid">小时</span>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>活动开始时间：</label>
        <div class="layui-input-block len-mid">
            <input type="text" class="layui-input" name="start_time" lay-verify="required" id="start_time" autocomplete="off" readonly>
            <i class=" iconrili iconfont calendar"></i>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>活动结束时间：</label>
        <div class="layui-input-block len-mid end_time">
            <input type="text" class="layui-input" name="end_time" lay-verify="required|time" id="end_time" autocomplete="off" readonly>
            <i class=" iconrili iconfont calendar"></i>
        </div>
        <div class="word-aux">
            <p>结束时间不能小于开始时间，也不能小于当前时间</p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>是否模拟好友：</label>
        <div class="layui-input-block">
            <input type="radio" name="is_simulation" value="1" title="是">
            <input type="radio" name="is_simulation" value="0" title="否" checked>
        </div>
        <div class="word-aux">
            <p>说明：模拟好友指在规定时间未成团时，系统会在截止时间补充虚拟会员促使成团，该团不会组合失败。</p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>仅新人参与限制：</label>
        <div class="layui-input-block">
            <input type="radio" name="is_new" value="1" title="是">
            <input type="radio" name="is_new" value="0" title="否" checked>
        </div>
        <div class="word-aux">
            <p>说明：新人指未参与过该活动的会员。</p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>瓜分方式：</label>
        <div class="layui-input-block">
            <input type="radio" name="divide_type" value="0" title="固定金额" checked>
            <input type="radio" name="divide_type" value="1" title="随机金额">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>余额设置：</label>
        <div class="layui-input-block">
            <input type="radio" name="balance_set" value="1" title="储值余额" checked>
            <input type="radio" name="balance_set" value="2" title="现金余额">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">红包图片：</label>
        <div class="layui-input-block img-upload">
            <div class="upload-img-block">
                <div class="upload-img-box">
                    <div class="upload-default" id="couponImg">
                        <div class="upload">
                            <i class="iconfont iconshangchuan"></i>
                            <p>点击上传</p>
                        </div>
                    </div>
                    <div class="operation">
                        <div>
                            <i title="图片预览" class="iconfont iconreview js-preview" style="margin-right: 20px;"></i>
                            <i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
                        </div>
                        <div class="replace_img js-replace">点击替换</div>
                    </div>
                    <input type="hidden" name="image"/>
                </div>
            </div>
        </div>
        <div class="word-aux">
            <p>建议尺寸：325*95像素，图片上传默认不限制大小</p>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>活动规则：</label>
        <div class="layui-input-inline">
            <textarea name="remark" class="layui-textarea len-long" value=""  lay-verify="required" maxlength="150"></textarea>
        </div>
    </div>

    <div class="form-row">
        <button class="layui-btn" lay-submit lay-filter="save">保存</button>
        <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
        <a id="image"></a>
    </div>

</div>
{/block}
{block name="script"}
<script>
    var saveData = null;
    var totalUploadNum = 0;
    var completeUploadNum = 0;
    var upload;

    layui.use(['form', 'laydate', 'form'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            repeat_flag = false; //防重复标识
        currentDate = new Date();  //当前时间
        form.render();

        currentDate.setDate(currentDate.getDate() + 30);   //当前时间+30之后的时间戳

        // 时间模块
        laydate.render({
            elem: '#validity_end_time', //指定元素
            type: 'datetime',
            value: currentDate,
            done: function (value) {
                $('.time-aaa').html('有效期：' + value);
            }
        });

        // 开始时间
        laydate.render({
            elem: '#start_time',//指定元素
            type: 'datetime',
            value: new Date(),
            done: function (value) {
                minDate = value;
                reRender();
            }
        });

        //结束时间
        laydate.render({
            elem: '#end_time',//指定元素
            type: 'datetime',
            value: new Date(currentDate)
        });

        /**
         * 重新渲染结束时间
         * */
        function reRender() {
            $("#end_time").remove();
            $(".end_time").html('<input type="text" id="end_time" name="end_time" placeholder="请输入结束时间" lay-verify="required|time" class="layui-input len-mid" autocomplete="off">');
            laydate.render({
                elem: '#end_time',
                type: 'datetime',
                min: minDate
            });
        }

        //监听瓜分次数限制
        form.on('radio(divide_frequency)', function (data) {
            var value = data.value;
            if (value == 0) {
                $('#divide_frequency_limit').hide();
            }
        });

        /**
         * 表单验证
         */
        form.verify({
            len: function (value) {
                if (value.length > 25) {
                    return "活动名称最多为25个字符!";
                }
            },
            money: function (value) {
				if(value<=0){
					return "瓜分金额不能小于等于0";
				}
                var arrMen = value.split(".");
                var val = 0;
                if (arrMen.length == 2) {
                    val = arrMen[1];
                }
                if (val.length > 2) {
                    return '保留小数点后两位'
                }
				
            },
            divide_num:function (value){
                if(value <= 1){
                    return '瓜分人数必须大于1人';
                }
            },
            time: function (value) {
                var now_time = (new Date()).getTime();
                var start_time = (new Date($("#start_time").val())).getTime();
                var end_time = (new Date(value)).getTime();
                if (now_time > end_time) {
                    return '结束时间不能小于当前时间!'
                }
                if (start_time > end_time) {
                    return '结束时间不能小于开始时间!';
                }
            },
            count: function (value) {
                if (value % 1 != 0) {
                    return '请输入整数';
                }
                if (value <= 0) {
                    return '数量不能小于0';
                }
            },
            divide_time:function (value){
                if (value > 24) {
                    return '有效期不能大于24小时';
                }
				if(value<=0){
					 return '有效期不能小于等于0';
				}
				if (value % 1 != 0) {
				    return '请输入整数';
				}
            },
			hongNum:function(value){
				if(value<=0){
					return "红包总量不能小于等于0"
				}
				if (value % 1 != 0) {
				    return '请输入整数';
				}
			}
        });

        upload = new Upload({
            elem: '#couponImg',
            auto:false,
            bindAction:'#image',
            callback: function(res) {
                uploadComplete('image', res.data.pic_path);
            }
        });

        function uploadComplete(field, pic_path) {
            saveData.field[field] = pic_path;
            completeUploadNum += 1;
            if(completeUploadNum == totalUploadNum){
                saveFunc();
            }
        }

        function saveFunc(){
            var data = saveData;

            // 删除图片
            if (!data.field.image) upload.delete();

            $.ajax({
                url: ns.url("hongbao://shop/hongbao/add"),
                data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function (res) {
                    repeat_flag = false;

                    if (res.code == 0) {
                        layer.confirm('添加成功', {
                            title: '操作提示',
                            btn: ['返回列表', '继续添加'],
                            closeBtn: 0,
                            yes: function () {
                                location.href = ns.url("hongbao://shop/hongbao/lists")
                            },
                            btn2: function () {
                                location.href = ns.url("hongbao://shop/hongbao/add")
                            }
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        }

        /**
         * 监听提交
         */
        form.on('submit(save)', function (data) {

            if (data.field.is_show == undefined) {
                data.field.is_show = 0;
            }

            if (repeat_flag) return;
            repeat_flag = true;

            saveData = data;
            var obj = $("img.img_prev[data-prev='1']");
            totalUploadNum = obj.length;
            if(totalUploadNum > 0){
                obj.each(function(){
                    var actionId = $(this).attr('data-action-id');
                    $(actionId).click();
                })
            }else{
                saveFunc();
            }
        });

    });

    function back() {
        location.href = ns.url("hongbao://shop/hongbao/lists");
    }
</script>
{/block}